﻿using Microsoft.AspNetCore.Html;
using Microsoft.AspNetCore.Mvc.Rendering;
using System.Collections.Generic;
using System.Text;

namespace Common
{
    public static class HtmlHelperExtensions
    {
        /// <summary>
        /// LayuiSelect渲染
        /// </summary>
        /// <param name="htmlHelper"></param>
        /// <param name="apiUrl">接口地址</param>
        /// <param name="elementId">dom元素Id</param>
        /// <param name="dictionary">key=参数,value=值</param>
        /// <param name="IsAsync">是否异步，默认：true</param>
        /// <param name="valueType">值类型：value:值，text:文本</param>
        /// <returns></returns>
        public static IHtmlContent LayuiRenderSelect(this Microsoft.AspNetCore.Mvc.Rendering.IHtmlHelper htmlHelper, string apiUrl, string elementId, IDictionary<string, object> dictionary = null, bool IsAsync = true, string valueType = "value")
        {
            string parms = string.Empty;
            if (dictionary != null)
            {
                foreach (var d in dictionary)
                {
                    parms += d.Key + ":" + "'" + d.Value + "',";
                }
                parms = parms.TrimEnd(',');
            }

            StringBuilder str = new StringBuilder();
            //     str.AppendFormat(@"
            //$.get('{0}', {{
            //         {1}
            //     }}, function (result) {{
            //         var select = document.getElementById('{2}');
            //         for(var p in result)
            //       {{
            //           var option = document.createElement('option');
            //           option.setAttribute('value', result[p].value);
            //           option.innerText = result[p].text;
            //           select.appendChild(option);
            //           form.render('select');
            //       }}
            //   }}, 'json')", apiUrl, parms, elementId);
            str.AppendFormat(@"$.ajax({{
                                    url: '{0}',
                                    type: 'GET',
                                    data: {{
                                               {1}
                                           }},
                                    dataType:'json',
                                    async:{3},
                                    success: function (result) {{
                                           var select = document.getElementById('{2}');
                                           for(var p in result)
                                           {{
                                                var option = document.createElement('option');
                                                option.setAttribute('value', result[p].{4});
                                                option.innerText = result[p].text;
                                                select.appendChild(option);
                                            }}
                                            form.render('select');
                                    }}
                                }})", apiUrl, parms, elementId, IsAsync.ToString().ToLower(), valueType);
            return htmlHelper.Raw(str.ToString());

        }

        /// <summary>
        /// 增加Html标签
        /// </summary>
        /// <param name="htmlContent"></param>
        /// <param name="dic"></param>
        /// <returns></returns>
        public static IHtmlContent HtmlAttributesCustom(this IHtmlContent htmlContent, Dictionary<string, string> dic)
        {

            TagBuilder content = (TagBuilder)htmlContent;
            foreach (var item in dic)
            {
                content.Attributes.Add(item);
            }
            return content;

        }
    }
}

